package com.hongkang.springtrains.web;

import com.hongkang.springtrains.model.User;
import org.springframework.http.HttpEntity;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/users")
public class RestController {

    public RestController(){
        System.out.println("RestController");
    }


    @RequestMapping(value = "/tom/{name}/{email}")
    @ResponseBody
    public String getUserA(@PathVariable("name")String name, @PathVariable("email")String email){
        System.out.println("getUserA");
        User user = new User();
        user.setName(name);
        user.setEmail(email);
        user.setID(100L);
        System.out.println(user);
        return "杜";
    }


    @RequestMapping(value = "/tom")
    @ResponseBody
    public User getUserA2(User user){
        System.out.println("getUserA2");
        System.out.println(user);
        return  user;
    }


    @RequestMapping(value = "/userB")
    public HttpEntity<String> getUserB(HttpEntity<String> requestEntity){
        System.out.println("getUserB");
        System.out.println(requestEntity.getBody());
        HttpEntity<String> responseEntity = new HttpEntity("This is body");
        return  responseEntity;
    }


    @ResponseBody
    @RequestMapping(value = "/userC",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public User getUserC(@Validated @RequestBody User user, @CookieValue(name = "username",required = false) String username){
        System.out.println("UserC");
        System.out.println(username);
        System.out.println(user);
        user.setID(100L);
        return user;
    }

}
